package com.frame.work.frame.deal.device0fac;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.frame.deal.base.BaseFrame;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 空调伴侣
 *
 * @author wjming
 */
@Data
public class AcCompanion0Fac0503 extends BaseFrame {
    /**
     * 是否支持通用绑定
     */
    private String spreadingType;

    /**
     * 开关状态
     */
    private String switchState;

    private String windSpeed;
    private String temperature;
    private String mode;


    public AcCompanion0Fac0503(String gatewayMac, String data) {
        super(gatewayMac, data);
        this.switchState = getSwitchState(data);
        this.spreadingType = getSpreadingType(data);
        this.windSpeed = getWindSpeed(data);
        this.temperature = getTemperature(data);
        this.mode = getAcModel(data);

        setActions(actions());
        String json = JSONObject.toJSONString(this);
        JsonToMqtt.sendMQTT(gatewayMac, json);
    }


    /**
     * 获取通用绑定信息
     *
     * @return
     */
    private String getSpreadingType(String data) {
        return StringUtils.substring(data, 38, 40);
    }


    /**
     * 获取开关状态
     *
     * @return
     */
    private String getSwitchState(String data) {
        return StringUtils.substring(data, data.length() - 10, data.length() - 8);
    }

    private String getAcModel(String data) {
        return StringUtils.substring(data, data.length() - 8, data.length() - 6);
    }



    /**
     * 获取风速
     *
     * @param data
     * @return
     */
    private String getWindSpeed(String data) {
        return StringUtils.substring(data, data.length() - 6, data.length() - 4);
    }

    /**
     * 获取温度信息
     *
     * @param data
     * @return
     */
    private String getTemperature(String data) {
        return StringUtils.substring(data, data.length() - 4, data.length() - 2);
    }

    private Map<String, Object> actions() {
        Map<String, Object> actions = new HashMap(3);
        actions.put("switchState", this.switchState);
        actions.put("windSpeed", this.windSpeed);
        actions.put("temperature", this.temperature);
        actions.put("mode", this.mode);
        return actions;
    }

}
